package com.yingcheng.numerology.framework.bazi.core.util;

import java.time.LocalDate;
import java.time.MonthDay;
import java.util.HashMap;
import java.util.Map;

public class BaziMonthPillarCalculator {

    // 地支数组
    private static final String[] EARTHLY_BRANCHES = {
            "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子", "丑"
    };

    // 天干数组
    private static final String[] HEAVENLY_STEMS = {
            "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"
    };

    // 年干正月对应天干映射表
    private static final Map<String, Integer> YEAR_HEAVENLY_STEMS_MAPPING = new HashMap<>();

    static {
        YEAR_HEAVENLY_STEMS_MAPPING.put("甲", 2); // 丙
        YEAR_HEAVENLY_STEMS_MAPPING.put("己", 2); // 丙
        YEAR_HEAVENLY_STEMS_MAPPING.put("乙", 4); // 戊
        YEAR_HEAVENLY_STEMS_MAPPING.put("庚", 4); // 戊
        YEAR_HEAVENLY_STEMS_MAPPING.put("丙", 6); // 庚
        YEAR_HEAVENLY_STEMS_MAPPING.put("辛", 6); // 庚
        YEAR_HEAVENLY_STEMS_MAPPING.put("丁", 8); // 壬
        YEAR_HEAVENLY_STEMS_MAPPING.put("壬", 8); // 壬
        YEAR_HEAVENLY_STEMS_MAPPING.put("戊", 0); // 甲
        YEAR_HEAVENLY_STEMS_MAPPING.put("癸", 0); // 甲
    }

    // 节气起点
    private static final Map<String, MonthDay> SOLAR_TERMS = new HashMap<>();

    static {
        SOLAR_TERMS.put("立春", MonthDay.of(2, 4));
        SOLAR_TERMS.put("惊蛰", MonthDay.of(3, 6));
        SOLAR_TERMS.put("清明", MonthDay.of(4, 5));
        SOLAR_TERMS.put("立夏", MonthDay.of(5, 5));
        SOLAR_TERMS.put("芒种", MonthDay.of(6, 6));
        SOLAR_TERMS.put("小暑", MonthDay.of(7, 7));
        SOLAR_TERMS.put("立秋", MonthDay.of(8, 7));
        SOLAR_TERMS.put("白露", MonthDay.of(9, 7));
        SOLAR_TERMS.put("寒露", MonthDay.of(10, 8));
        SOLAR_TERMS.put("立冬", MonthDay.of(11, 7));
        SOLAR_TERMS.put("大雪", MonthDay.of(12, 7));
    }

    public static String calculateMonthPillar(String yearHeavenlyStem, LocalDate birthDate) {
        // 确定节气对应的月份
        int lunarMonth = getLunarMonthFromSolarTerms(birthDate);

        // 获取年干对应的正月天干起始点
        Integer heavenlyStemStart = YEAR_HEAVENLY_STEMS_MAPPING.get(yearHeavenlyStem);

        if (heavenlyStemStart == null) {
            throw new IllegalArgumentException("无效的年干输入：" + yearHeavenlyStem);
        }

        // 推算月柱天干
        int heavenlyStemIndex = (heavenlyStemStart + lunarMonth - 1) % 10;
        String heavenlyStem = HEAVENLY_STEMS[heavenlyStemIndex];

        // 推算月柱地支
        String earthlyBranch = EARTHLY_BRANCHES[lunarMonth - 1];

        return heavenlyStem + earthlyBranch;
    }

    private static int getLunarMonthFromSolarTerms(LocalDate birthDate) {
        int year = birthDate.getYear();
        int monthIndex = 0;

        for (Map.Entry<String, MonthDay> entry : SOLAR_TERMS.entrySet()) {
            MonthDay solarTerm = MonthDay.from(entry.getValue().atYear(year));
            if (birthDate.isBefore(solarTerm.atYear(year))) {
                return monthIndex == 0 ? 12 : monthIndex; // 如果在立春前，属腊月
            }
            monthIndex++;
        }
        return 12; // 默认腊月
    }

    public static void main(String[] args) {
        // 示例：某人生于甲子年，公历日期为 2024 年 3 月 5 日
        String yearHeavenlyStem = "甲";
        LocalDate birthDate = LocalDate.of(2024, 3, 5);

        String monthPillar = calculateMonthPillar(yearHeavenlyStem, birthDate);
        System.out.println("月柱为: " + monthPillar); // 输出月柱
    }
}
